import { defineStore } from 'pinia';

const initState = {
  isLogined: false, // 是否登录
  userInfo: {} // 用户信息
};

export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref(initState.userInfo);

    const setUserInfo = userInfo => {
      userInfo.value = userInfo;
    };

    const clearUserInfo = () => {
      userInfo.value = initState.userInfo;
    };

    const isLogined = computed(() => {
      return !!userInfo.value.token;
    });

    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
      isLogined
    };
  },
  {
    persist: {
      enabled: true
    }
  }
);
